package Client.GUI;
/**
 * 
 * /**
 * 
 * @author Baglama Team
 *
 *
 */
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.JFileChooser;
import Client.logic.ClientIF;
import Client.logic.MyFile;
import Server.DataBase.Lesson;
import Server.Message.MessageAddTask;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageTeacherLesson;

import javax.swing.JComboBox;
/**
  
teacher initializes add class add task panel
 */
public class AddTaskPanel extends MyJPanel {
	/**
	 * private static final long serialVersionUID = 1L;
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox comboBoxLesson, comboBoxMonth, comboBoxDay, comboYear;
	private String dir, filename;
	private Lesson lesson;
	private int month, day, year;
	private JButton btnAdd, btnChoose, btnReset;
	/**
	 ** This is the  constructor
	 *@param client
	 */
	public AddTaskPanel(ClientIF client) {
		super(PanelType.ADD_TASK_PANEL,client);
		setLayout(null);
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"add task.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(93, 0, 408, 113);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		init();
	}
	/** 
	 * This method initializes add task panel
	 * */
	public void init(){
		initComboBox();
		initLabels();
		initBtn();

		
		getClient().sendMsgToServer(new MessageTeacherLesson(getClient().getUser().getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
		
		comboBoxLesson.addItem("Choose");
		for (Lesson lesson : msg.getArray())
			comboBoxLesson.addItem(lesson);
	}
	
	private void initLabels(){
		JLabel lblNewLabel = new JLabel("Select Lesson");
		lblNewLabel.setBounds(43, 115, 85, 14);
		add(lblNewLabel);
		
		JLabel lblDueDate = new JLabel("Submission Date:");
		lblDueDate.setBounds(43, 210, 107, 14);
		add(lblDueDate);
		
		JLabel lblMounth = new JLabel("Month");
		lblMounth.setBounds(174, 210, 46, 14);
		add(lblMounth);
		
		JLabel lblDay = new JLabel("Day");
		lblDay.setBounds(273, 210, 46, 14);
		add(lblDay);
		
		JLabel lblYear = new JLabel("Year");
		lblYear.setBounds(388, 210, 46, 14);
		add(lblYear);
		
		JLabel lblSelectFile = new JLabel("Select File");
		lblSelectFile.setBounds(43, 161, 71, 14);
		add(lblSelectFile);
		
	}
	
	private void initBtn(){
		btnChoose = new JButton("Choose");
		btnChoose.addActionListener(new ChooseFile());
		btnChoose.setBounds(149, 157, 89, 23);
		add(btnChoose);
		
		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new sendFile());
		btnAdd.setEnabled(false);
		btnAdd.setBounds(119, 274, 89, 23);
		add(btnAdd);
		
		btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				reset();
			}
		});
		btnReset.setBounds(243, 274, 89, 23);
		add(btnReset);
	}
	
	public void initComboBox(){
		comboBoxMonth = new JComboBox();
		comboBoxMonth.setBounds(221, 207, 40, 20);
		comboBoxMonth.setEnabled(false);
		for (int i=1; i<=12; i++) comboBoxMonth.addItem(i);
		comboBoxMonth.addActionListener(new MonthListener());
		add(comboBoxMonth);
		
		comboBoxDay = new JComboBox();
		comboBoxDay.setBounds(317, 207, 46, 20);
		comboBoxDay.setEnabled(false);
		add(comboBoxDay);
		
		comboYear = new JComboBox();
		comboYear.setBounds(437, 207, 89, 20);
		comboYear.setEnabled(false);
		comboYear.addActionListener(new YearListener());
		add(comboYear);
		
		comboBoxLesson = new JComboBox();
		comboBoxLesson.setBounds(149, 112, 128, 20);
		comboBoxLesson.addActionListener(new LessonListener());
		add(comboBoxLesson);
	}
	
	private void reset(){
		getClient().swapFromBack(pushPanel());
	}
	
	
	/** 
	 *l
	 * */
	public MyJPanel pushPanel() {
		return new AddTaskPanel(getClient());
	}
	/** 
	 *teacher choose file to add task
	 * */
	class ChooseFile implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JFileChooser c = new JFileChooser();
			int rVal = c.showOpenDialog((Component) getClient());
			if (rVal == JFileChooser.APPROVE_OPTION) {
				filename = c.getSelectedFile().getName();
				dir = c.getCurrentDirectory().toString();
				comboBoxMonth.setEnabled(true);
			}
			if (rVal == JFileChooser.CANCEL_OPTION) {
				filename = null;
				dir = null;
			}
	    }
	 }
	/** 
	 *teacher send file  add task
	 * */
	 class sendFile implements ActionListener{
		 public void actionPerformed(ActionEvent e) {
			 String LocalfilePath=dir+File.separator+filename;
			 try{
				 File newFile = new File (LocalfilePath);  		      
			     byte [] mybytearray  = new byte [(int)newFile.length()];
			     FileInputStream fis = new FileInputStream(newFile);
			     BufferedInputStream bis = new BufferedInputStream(fis);			  
			     bis.read(mybytearray,0,mybytearray.length);
			     MyFile msg= new MyFile(filename, mybytearray);
			     getClient().sendMsgToServer(new MessageAddTask(lesson, day, month, year, msg));
			     reset();
			    }
			catch (Exception e1) {
				e1.printStackTrace();
			}
		 }
	 }
	 
	 private class LessonListener implements ActionListener{
			public void actionPerformed(ActionEvent event) {
				try{
				lesson = (Lesson) comboBoxLesson.getSelectedItem();	
				}catch (java.lang.Throwable t){}
			}
	}
	 /** 
		 *teacher choose date submitted   task
		 * */
	 private class MonthListener implements ActionListener{
			public void actionPerformed(ActionEvent event) {
				try{
					month = (Integer) comboBoxMonth.getSelectedItem();
					comboBoxDay.removeAllItems();
					for(int i=1; i<=28; i++) comboBoxDay.addItem(i);
					if (month!=2){
						comboBoxDay.addItem(29);
						comboBoxDay.addItem(30);
						if (month==1 || month==3 || month==5 || month==7 || month==8 || month == 10 || month==12) comboBoxDay.addItem(31);
					}
					comboBoxDay.setEnabled(true);
					comboBoxDay.addActionListener(new DayListener());
				} catch (java.lang.Throwable t){}
			}
		}
	 
	 private class DayListener implements ActionListener{
			public void actionPerformed(ActionEvent event) {
				try{
					day = (Integer) comboBoxDay.getSelectedItem();
					Calendar cal = Calendar.getInstance();
					int current = cal.get(Calendar.YEAR);
					comboYear.removeAllItems();
					comboYear.setEnabled(true);
					comboYear.addItem(current);
					comboYear.addItem(current+1);
					comboYear.addActionListener(new YearListener());
				} catch (java.lang.Throwable t){}
			}
	}
	 
	private class YearListener implements ActionListener{
			public void actionPerformed(ActionEvent event) {
				try{
					year = (Integer) comboYear.getSelectedItem();
					btnAdd.setEnabled(true);
				} catch (java.lang.Throwable t){}
			}
	}
}

